/**
 * @author 黄波
 * 创建日期：2011Oct 11, 2011
 * TODO// N/A
 */
package com.platform.njcc.action;

import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts2.ServletActionContext;

import com.platform.constant.ResponseCode;
import com.platform.os.util.Util;
import com.platform.pojo.UserInfo;
import com.platform.user.bo.UserBo;

/**
 * 更新用户资料
 */
public class UpdateUserInfoAction extends NJCCSuport {

	private UserInfo user;
	private boolean success;
	private String msg;
	public UserInfo getUser() {
		return user;
	}
	public void setUser(UserInfo user) {
		this.user = user;
	}
	public boolean isSuccess() {
		return success;
	}
	public void setSuccess(boolean success) {
		this.success = success;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}

	@Override
	public String execute() throws Exception {
		HttpSession session = ServletActionContext.getRequest().getSession();
		UserInfo user = (UserInfo) session.getAttribute("user");
		int errorCode = 0;
		if(user == null) {
			msg = ResponseCode.FAILURE_UPDATE;
		} else {
			UserBo bo = UserBo.getInstance();
//			BeanUtils.copyProperties(user, this.user);
			if(this.getUser().getRealName() != null && !this.getUser().getRealName().trim().equals("")) {
				user.setRealName(this.getUser().getRealName());
			}
			UserInfo dbUser = null;
			if(this.getUser().getEmail() != null && !this.getUser().getEmail().trim().equals("")) {
				dbUser = bo.getUserByEamil(getUser().getEmail());
				if(dbUser == null || (dbUser != null && dbUser.getId().intValue() == user.getId().intValue())) {
					user.setEmail(this.getUser().getEmail());
				} else {
					errorCode = 101;//邮箱重复
				}
			}
			if(errorCode == 0 && this.getUser().getMobile() != null && !this.getUser().getMobile().trim().equals("")) {
				dbUser = bo.getUserByMobile(getUser().getMobile());
				if(dbUser == null || (dbUser != null && dbUser.getId().intValue() == user.getId().intValue())) {
					user.setMobile(this.getUser().getMobile());
				} else {
					errorCode = 102;//手机号重复
				}
			}
			if(errorCode == 0) {
				if(bo.updateUser(user)) {
					msg = ResponseCode.SUCCESS_UPDATE;
				} else {
					msg = ResponseCode.FAILURE_DB_ERROR;
				}
			} else {
				if(errorCode == 101) {
					msg = "邮箱已被占用！";
				} else if(errorCode == 102) {
					msg = "手机号已被占用！";
				}
			}
		}
		success = true;
		HashMap map = new HashMap();
		map.put("msg", msg);
		map.put("success", success);
		JSONObject json = JSONObject.fromObject(map);
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		out.println(json);
		out.flush();
		out.close();
		return null;
	}
}
